//
//  ClubSquareTopicView.swift
//  77Club
//
//  Created by Apple on 2024/12/25.
//

import UIKit

class ClubSquareTopicCateView: UIView, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

    private let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
    
    var models: [ClubDynamicGameModel]?{
        didSet{
            collectionView.reloadData()
        }
    }
    /// 话题主页
    var toTopicDetailVC: ((_ topicId: String) -> Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        subviews{
            collectionView
        }
        
        collectionView.leading(12.0.Scale).trailing(0).height(30.0.Scale).centerVertically()
        
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 8.0.Scale
        layout.minimumInteritemSpacing = 8.0.Scale
        layout.scrollDirection = .horizontal
//        layout.sectionInset = UIEdgeInsets(top: 0, left: 12.0.Scale, bottom: 0, right: 0)
        
        collectionView.collectionViewLayout = layout
        collectionView.backgroundColor = .clear
        collectionView.register(ClubSquareTopicCateCell.self, forCellWithReuseIdentifier: ClubSquareTopicCateCell.description())
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.delegate = self
        collectionView.dataSource = self
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
       return models?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ClubSquareTopicCateCell.description(), for: indexPath) as? ClubSquareTopicCateCell else { return UICollectionViewCell() }
        cell.model = models?[indexPath.row]
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        toTopicDetailVC?(models?[indexPath.row].id ?? "")
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
        let name = "# " + (models?[indexPath.row].name ?? "")
        let font = 16.font_semiBold
        let attributedText = NSAttributedString(string: name, attributes: [NSAttributedString.Key.font: font as Any])
        
        // 计算文本宽度
        let size = attributedText.boundingRect(with: CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude), options: .usesLineFragmentOrigin, context: nil).size
        let width = size.width + 32.0.Scale
        
        return CGSize(width: width, height: 30.0.Scale)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
}


//MARK: 话题分类cell
class ClubSquareTopicCateCell: UICollectionViewCell {
    
    private let nameLabel = c_make_ex(UILabel.self)
        .color("#7D7E80").font(15.font_medium)
        .alignment(.center).corner(15.0.Scale).end()
    
    var model: ClubDynamicGameModel?{
        didSet{
            guard let md = model else { return }
            nameLabel.text = "# " + md.name
            if md.isSel {
                
                nameLabel.textColor = "#323233".toColor
                nameLabel.font = 16.font_semiBold
                nameLabel.layer.borderColor = "#323233".toColor.cgColor
                nameLabel.layer.borderWidth = 1.5
                nameLabel.layer.masksToBounds = true
            }else{
                
                nameLabel.textColor = "#7D7E80".toColor
                nameLabel.font = 15.font_semiBold
                nameLabel.layer.borderColor = "#EBEBEB".toColor.cgColor
                nameLabel.layer.borderWidth = 1.0
                nameLabel.layer.masksToBounds = true
            }
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.backgroundColor = .clear
        backgroundColor = .clear
        
        contentView.subviews{
            nameLabel
        }

        nameLabel.fillContainer()
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
